package cn.wgx.module.sys.service;

import cn.wgx.common.base.IBaseService;
import cn.wgx.common.entity.sys.Permission;
import cn.wgx.module.sys.mapper.PermissionMapper;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service
public class PermissionService extends IBaseService<PermissionMapper, Permission> {


    public Set<Permission> findPerByUserId(Long userId) {
        return baseMapper.getPerByUserId(userId);
    }

    public Set<Permission> findByRoleIds(Set<Long> roleIds) {
        if (roleIds == null || roleIds.isEmpty()) {
            return EMPTY_SET;
        }
        return baseMapper.getPerByRoleIds(roleIds);
    }

    /**
     * 变树
     *
     * @param permissions
     * @return
     */
    public static JSONArray sortPermission(List<Permission> permissions) {
        JSONArray jsonArray = new JSONArray();
        JSONArray jsonArray_tmp = new JSONArray();
        for (int n = permissions.size(), i = 0; i < n; i++) {
            Permission permission = permissions.get(i);
            if (permission.getParent_key().equals("0")) {
                JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSON(permission).toString());
                jsonArray.add(jsonObject);
                jsonArray_tmp.add(jsonObject);
                permissions.remove(i);
                i--;
                n = permissions.size();
            }
        }
        addPermission(permissions, jsonArray_tmp);
        return jsonArray;
    }


    private static void addPermission(List<Permission> permissions, JSONArray jsonArray_tmp) {
        int len = permissions.size();
        for (int n = len, i = 0; i < n; i++) {
            Permission permission = permissions.get(i);
            for (Object o : jsonArray_tmp) {
                JSONObject s = (JSONObject) o;
                if (permission.getParent_key().equals(s.getString("permissionKey"))) {
                    JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSON(permission).toString());
                    JSONArray jsonArray1 = s.getJSONArray("children");
                    if (jsonArray1 == null) {
                        jsonArray1 = new JSONArray();
                        s.put("children", jsonArray1);
                    }
                    jsonArray1.add(jsonObject);
                    jsonArray_tmp.add(jsonObject);
                    permissions.remove(i);
                    i--;
                    n = permissions.size();
                    break;
                }
            }
        }
        int len_end = permissions.size();
        if (len_end == 0 || len_end == len) {
            //结束
        } else {
            addPermission(permissions, jsonArray_tmp);
        }
    }

    @Transactional(readOnly = true)
    public List<Permission> getMenu() {
        //1.创建一个QueryWapper对象
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        //2.构造查询条件
        queryWrapper.eq("menu_flag", "Y");
        //3.执行查询
        List<Permission> permissions = baseMapper.selectList(queryWrapper);
        return permissions;
    }

//    @Transactional
//    public int updateRolePermission(List<RolePermission> rolePermissions, String access_token) {
//        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("role_id", rolePermissions.get(0).getRole_id());
////        baseMapper.delete(queryWrapper);
//        Date date = new Date();
//        User user = SysUserUtil.getLoginAppUser(access_token);
//        int r = 0;
//        for (RolePermission rolePermission : rolePermissions) {
//            rolePermission.setUpdateDate(date);
//            rolePermission.setCreateDate(date);
//            if (user != null) {
//                rolePermission.setCreateUserId(user.getUser_id().intValue());
//                rolePermission.setUpdateUserId(user.getUser_id().intValue());
//            }
////            filter(sysRolePermission, true);
//            r += baseMapper.insert(rolePermission);
//        }
//        return r;
//    }
//    public List<RolePermission> getPers(Integer role_id){
//        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("role_id", role_id);
//        return baseMapper.selectList(queryWrapper);
//    }
}
